﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using DailyPhoto.Common.Services;
using DailyPhoto.Common;
using System.ServiceModel;

namespace DailyPhoto.Web
{
    public static class DailyPhotoQueue
    {
        private static readonly object sync = new object();
        private static Queue<DailyPhotoRequest> queue;
        private static Thread monitor;
        private static DateTime LastRequest = DateTime.MinValue;

        static DailyPhotoQueue()
        {
            queue = new Queue<DailyPhotoRequest>();

            monitor = new Thread( 
            () =>
            {
                while (true)
                {
                    DailyPhotoRequest req = null;

                    if (queue.Count > 0)
                    {
                        lock (sync)
                        {
                            if (queue.Count > 0)
                            {
                                req = queue.Dequeue();
                            }
                        }
                    }
                   
                    if (req != null)
                    {
                        if ((req.Timestamp - LastRequest).TotalMinutes >= 30)
                        {
                            GeneratePhoto();
                            LastRequest = req.Timestamp;
                        }
                    }

                    Thread.Sleep(1000);
                }
            });

            monitor.IsBackground =  true;
            monitor.Start();
        }



        private static void GeneratePhoto()
        {
            var bind = new NetTcpBinding(SecurityMode.None);
            bind.MaxReceivedMessageSize = 1024 * 1024 * 10;
            bind.ReaderQuotas.MaxArrayLength = 1024 * 1024 * 10;
            bind.MaxBufferSize = 1024 * 1024 * 10;
            bind.ReaderQuotas.MaxStringContentLength = 1024 * 1024 * 10;
            bind.ReceiveTimeout = new TimeSpan(TimeSpan.TicksPerHour * 6);
            bind.ReliableSession.InactivityTimeout = new TimeSpan(TimeSpan.TicksPerHour * 6);

            var address = string.Format("net.tcp://{0}:{1}/dailyphoto",
                                        (string)Application.Config.server.host,
                                        (string)Application.Config.server.port);

            using (var channel = new ChannelFactory<IDailyPhoto>(bind, new EndpointAddress(address)))
            {
                var proxy = channel.CreateChannel();
                proxy.GeneratePhoto();
            }
        }


        public static void Enqueue(DailyPhotoRequest req)
        {
            lock (sync)
            {
                queue.Enqueue(req);
            }
        }
    }

    public class DailyPhotoRequest
    {
        public DailyPhotoRequest()
        {
            Timestamp = DateTime.Now;
        }

        public DateTime Timestamp { get; set; }
        public string Username { get; set; }
    }
}